<template>
  <div class="app-container">
    <el-row
      :gutter="15"
      class="mb8"
    >
      <el-col>
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          @click="handleAdd({ type: 'substation' })"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table
      :data="substationList"
      :height="tableHeight"
      border
      stripe
      highlight-current-row
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table
            :border="childBorder"
            :data="props.row.connectionPointList"
            :header-cell-style="{ background: '#dadde7', textSize: '14px' }"
            :highlight-current-row="false"
            stripe
            border
          >
            <el-table-column width="40"></el-table-column>
            <el-table-column
              align="center"
              label="并网点名称"
              prop="connectionPointName"
            ></el-table-column>
            <el-table-column
              align="center"
              label="并网点容量"
              prop="capacity"
            ></el-table-column>
            <el-table-column
              align="center"
              label="电压等级"
              prop="voltageLevel"
            ></el-table-column>
            <el-table-column
              align="center"
              label="电流变比"
              prop="location"
            ></el-table-column>
            <el-table-column
              align="center"
              label="具体位置"
              prop="currentRatio"
            ></el-table-column>
            <el-table-column
              align="center"
              label="经度"
              prop="lon"
            ></el-table-column>
            <el-table-column
              align="center"
              label="维度"
              prop="lat"
            ></el-table-column>

            <el-table-column
              align="center"
              label="操作"
            >
              <template slot-scope="scope">
                <el-button
                  icon="el-icon-edit"
                  type="text"
                  @click="
                    handleEdit({ type: 'connectionPoint', num: scope.row.num })
                  "
                >修改
                </el-button>
                <el-button
                  icon="el-icon-delete"
                  type="text"
                  @click="
                    handleDelete({
                      type: 'connectionPoint',
                      num: scope.row.num,
                    })
                  "
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column
        label="变电站名称"
        prop="substationName"
        width="120"
      ></el-table-column>
      <el-table-column
        align="center"
        label="地区"
        prop="ancestorNames"
        width="200"
      ></el-table-column>
      <el-table-column
        align="center"
        label="位置"
        prop="location"
        width="240"
      ></el-table-column>
      <el-table-column
        align="center"
        label="公司名称"
        prop="companyName"
        show-overflow-tooltip
        width="120"
      ></el-table-column>
      <el-table-column
        align="center"
        label="联系人"
        prop="contactName"
        width="100"
      ></el-table-column>
      <el-table-column
        align="center"
        label="电话"
        prop="phone"
        width="100"
      ></el-table-column>
      <el-table-column
        align="center"
        label="总容量"
        prop="totalCapacity"
        width="100"
      ></el-table-column>
      <el-table-column
        align="center"
        label="并网点数量"
        prop="pointsAmount"
        width="100"
      ></el-table-column>
      <el-table-column
        align="center"
        label="规模描述"
        prop="scaleDesc"

      ></el-table-column>
      <el-table-column
        align="center"
        fixed="right"
        label="操作"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleAdd({ type: 'connectionPoint' })"
          >新增并网点
          </el-button>
          <el-button
            icon="el-icon-edit"
            type="text"
            @click="handleEdit({ type: 'substation', num: scope.row.num })"
          >修改
          </el-button>
          <el-button
            icon="el-icon-delete"
            type="text"
            @click="handleDelete({ type: 'substation', num: scope.row.num })"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--变电站对话框-->
    <el-dialog
      :title="substationTitle"
      :visible.sync="substationOpen"
      v-dialogDrag
      width="800px"
    >
      <el-form
        ref="substationFormDataRef"
        :model="substationFormData"
        :rules="substationRules"
        label-width="92px"
      >
        <!--第一行-->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item
              label="名称"
              prop="substationName"
            >
              <el-input
                v-model.trim="substationFormData.substationName"
                clearable
                placeholder="请输入变电站名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 下拉框 -->
            <el-form-item
              label="客户"
              prop="customer"
            >
              <el-select
                v-model="substationFormData.customer"
                clearable
                filterable
                placeholder="请选择客户"
              >
                <el-option
                  v-for="item in customerOptions"
                  :key="item.num"
                  :label="item.companyName"
                  :value="item.num"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第二行 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item
              label="地区"
              prop="area"
            >
              <treeselect
                v-model="substationFormData.area"
                :normalizer="normalizer"
                :options="areaOptions"
                placeholder="选择地区"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第三行 -->
        <el-row :gutter="15">
          <el-col :span="24">
            <!--textarea输入-->
            <el-form-item
              label="位置"
              prop="location"
            >
              <el-input
                v-model.trim="substationFormData.location"
                :maxlength="150"
                :rows="2"
                clearable
                placeholder="请输入变电站所在位置"
                resize="none"
                show-word-limit
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!--第四行-->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item
              label="总容量"
              prop="totalCapacity"
            >
              <el-input
                v-model.trim="substationFormData.totalCapacity"
                :disabled="disabled"
                clearable
                placeholder="请输入变电站总容量"
              >
                <template slot="append">KW</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="并网点数量"
              prop="pointsAmount"
            >
              <el-input
                v-model.trim="substationFormData.pointsAmount"
                :disabled="disabled"
                clearable
                placeholder="请输入变电站下并网点数量"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 第五行 -->
        <el-row :gutter="15">
          <el-col :span="24">
            <!--textarea输入-->
            <el-form-item
              label="规模描述"
              prop="scaleDesc"
            >
              <el-input
                v-model.trim="substationFormData.scaleDesc"
                :maxlength="150"
                :rows="2"
                clearable
                placeholder="请输入变电站规模描述"
                resize="none"
                show-word-limit
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitFormData"
        >提交</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </el-dialog>

    <!-- 并网点对话框 -->
    <el-dialog
      :title="connectionPointTitle"
      :visible.sync="connectionPointOpen"
    >
      <el-form
        ref="connectionPointFormDataRef"
        :model="connectionPointFormData"
        :rules="connectionPointRules"
        label-width="80px"
      >
        <!-- 第一行 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <!--并网点名称-->
            <el-form-item
              label="名称"
              prop="connectionPointName"
            >
              <el-input
                v-model.trim="connectionPointFormData.connectionPointName"
                clearable
                placeholder="并网点名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--客户-->
            <el-form-item
              label="客户"
              prop="customerNum"
            >
              <el-select
                v-model="connectionPointFormData.customerNum"
                clearable
                filterable
                placeholder="请选择客户"
              >
                <el-option
                  v-for="item in customerOptions"
                  :key="item.num"
                  :label="item.companyName"
                  :value="item.num"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
        </el-row>

        <!-- 第三行、第四行、第五行 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <!--变电站-->
            <el-form-item
              label="变电站"
              prop="substationNum"
            >
              <el-select
                v-model="connectionPointFormData.substationNum"
                clearable
                filterable
                placeholder="请选择变电站"
              >
                <el-option
                  v-for="item in substationList"
                  :key="item.num"
                  :label="item.substationName"
                  :value="item.num"
                ></el-option>
              </el-select>
            </el-form-item>
            <!-- 地区 -->
            <el-form-item
              label="地区"
              prop="area"
            >
              <treeselect
                v-model="connectionPointFormData.area"
                :normalizer="normalizer"
                :options="areaOptions"
                placeholder="选择地区"
              />

            </el-form-item>
            <!-- 位置 -->
            <el-form-item
              label="位置"
              prop="location"
            >
              <el-input
                v-model.trim="connectionPointFormData.location"
                :maxlength="100"
                :rows="2"
                clearable
                placeholder="输入并网点位置"
                resize="none"
                show-word-limit
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="容量"
              prop="capacity"
            >
              <el-input
                v-model.trim="connectionPointFormData.capacity"
                clearable
                placeholder="并网点容量"
              >
                <template slot="append">KW</template>
              </el-input>
            </el-form-item>
            <el-form-item
              label="电压等级"
              prop="voltageLevel"
            >
              <el-select
                v-model="connectionPointFormData.voltageLevel"
                placeholder="并网点电压等级"
              >
                <el-option
                  v-for="dict in voltageLevelOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictLabel"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="电流变比"
              prop="currentRatio"
            >
              <el-input
                v-model.trim="connectionPointFormData.currentRatio"
                clearable
                placeholder="并网点电流变比"
              >
                <!-- <template slot="append">KW</template> -->
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第二行 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <!--联系人-->
            <el-form-item
              label="联系人"
              prop="contactName"
            >
              <el-input
                v-model.trim="connectionPointFormData.contactName"
                clearable
                placeholder="联系人名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--联系人-->
            <el-form-item
              label="联系方式"
              prop="phone"
            >
              <el-input
                v-model.trim="connectionPointFormData.phone"
                clearable
                placeholder="联系方式"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第六行 -->
        <el-row :gutter="15">
          <el-col :span="12">
            <!--经度-->
            <el-form-item
              label="经度"
              prop="lon"
            >
              <el-input
                v-model.trim="connectionPointFormData.lon"
                clearable
                placeholder="经度"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!--维度-->
            <el-form-item
              label="维度"
              prop="lat"
            >
              <el-input
                v-model.trim="connectionPointFormData.lat"
                clearable
                placeholder="维度"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitFormData"
        >提交</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSubstationFold,
  getSubstationByNum,
  delSubstation,
  editSubstation,
  addSubstation,
} from "@/api/maintenance/substation";
import {
  getConnectionPointByNum,
  delConnectionPoint,
  editConnection,
  addConnectionPoint,
} from "@/api/maintenance/connectionPoint";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listArea } from "@/api/maintenance/area";
import { listCustomer } from "@/api/maintenance/customer";

export default {
  components: { Treeselect },
  data() {
    return {
      /**
       * 表格数据
       */
      substationList: [],
      tableHeight: "calc(100vh - 200px)",
      childBorder: false,

      /**
       * 变电站对话框
       */
      substationFormData: {},
      substationRules: [],
      disabled: false,
      substationTitle: "",
      substationOpen: false,

      /**
       * 并网点对话框
       */
      connectionPointFormData: {},
      connectionPointRules: [],
      connectionPointTitle: "",
      connectionPointOpen: false,
      /**
       * 操作类型参数
       */
      operationParam: {
        type: undefined,
        num: undefined,
      },
      // 地区下拉框
      areaOptions: [],
      //客户下拉框
      customerOptions: [],
      //电压等级
      voltageLevelOptions: [],
    };
  },
  created() {
    this.getList();
    // 查询地区列表
    listArea().then((response) => {
      this.areaOptions = this.handleTree(response.data, "num");
    });
    /**查询客户 */
    listCustomer().then((response) => {
      this.customerOptions = response.rows;
    });
    this.getDicts("zk_voltage_level").then((response) => {
      this.voltageLevelOptions = response.data;
    });
  },
  methods: {
    getList() {
      listSubstationFold().then((res) => {
        this.substationList = res.rows;
      });
    },
    // 新增变电站/出线
    handleAdd(operationParam) {
      if (operationParam.type === "substation") {
        this.resetForm("substationFormDataRef");
        this.substationTitle = "新增变电站";
        this.disabled = true;
        this.substationOpen = true;
      } else {
        this.resetForm("connectionPointFormDataRef");
        this.connectionPointTitle = "新增并网点";
        this.connectionPointOpen = true;
      }
    },
    // 编辑变电站/出线
    handleEdit(operationParam) {
      if (operationParam.type === "substation") {
        this.resetForm("substationFormDataRef");
        getSubstationByNum(operationParam.num).then((response) => {
          this.substationFormData = response.data;
        });
        this.substationTitle = "编辑变电站";
        this.substationOpen = true;
      } else {
        this.resetForm("connectionPointFormDataRef");
        getConnectionPointByNum(operationParam.num).then((response) => {
          this.connectionPointFormData = response.data;
        });
        this.connectionPointTitle = "修改并网点";
        this.connectionPointOpen = true;
      }
    },
    // 删除变电站/出线
    handleDelete(operationParam) {
      if (operationParam.type === "substation") {
        this.$modal
          .confirm("确认删除变电站吗？")
          .then(function () {
            return delSubstation(operationParam.num);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      } else {
        this.$modal
          .confirm("确认删除变电站吗？")
          .then(function () {
            return delConnectionPoint(operationParam.num);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      }
    },

    submitFormData() {
      if (this.operationParam.type === "substation") {
        this.$refs["substationFormDataRef"].validate((valid) => {
          if (valid) {
            if (this.substationFormData.num !== undefined) {
              //修改
              editSubstation(this.substationFormData).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.getList();
                this.open = false;
              });
            } else {
              //新增
              addSubstation(this.substationFormData).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.getList();
                this.open = false;
              });
            }
          }
        });
      } else {
        this.$refs["connectionPointFormDataRef"].validate((valid) => {
          if (valid) {
            if (this.connectionPointFormData.num !== undefined) {
              // 修改
              editConnection(this.connectionPointFormData).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.getList();
                this.open = false;
              });
            } else {
              // 新增
              addConnectionPoint(this.connectionPointFormData).then(
                (response) => {
                  this.$modal.msgSuccess("新增成功");
                  this.getList();
                  this.open = false;
                }
              );
            }
          }
        });
      }
    },
    close() {
      this.substationOpen = false;
      this.connectionPointOpen = false;

      this.resetForm("substationFormDataRef");
      this.resetForm("connectionPointFormDataRef");
    },
    /** 转换地区数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.num,
        label: node.name,
        children: node.children,
      };
    },
  },
};
</script>

<style lang="scss" scoped></style>
